import userService from './user.service';

export default {
  name: "tabComponents",
  inject: ['reload'],
  data() {
    return {
      tempName: '',
      tempPwd: '',
      nameError: false,
      pwdError: false,
      isHome: true,
      loadModule: false,
      islogin: false
    }
  },
  methods: {
    goToHomePage() {
      this.$router.push('/');
    },
    goToMaiowu() {
      this.$router.push('/miaowu');
    },
    goToPictrue() {
      this.$router.push('/album');
    },
    goToStarPage() {
      this.$router.push('star');
    },
    editAdmin() {
      this.$confirm('退出后将会返回首页', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
        localStorage.removeItem("MW__AIDMd5");
        this.$router.push('/');
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消退出'
        });
      });
    },
    loadOpenModule() {
      this.loadModule = true;
    },
    closeModal() {
      this.loadModule = false;
    },
    load() {
      this.nameError = false;
      this.pwdError = false;
      if (this.tempName === '') {
        this.nameError = true;
        return;
      }
      if (this.tempPwd === '') {
        this.pwdError = true;
        return;
      }
      let data = {
        name: this.tempName,
        pwd: this.tempPwd
      }
      userService.userLogin(data)
        .then(res => {
          if (res.data.login === 1) {
            localStorage.setItem("HOMEUSERNAME", JSON.stringify(res.data.list[0].name));
            this.islogin = true;
            this.loadModule = false;
            this.reload();
          }else {
            this.nameError = true;
            this.pwdError = true;
          }
        })
    },
    exitLoad() {
      this.$confirm('确定要退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem("HOMEUSERNAME");
        this.islogin = false;
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
        this.reload();
      }).catch(() => {
        console.log('cannel');
      });
    }
    // userRegister() {
    //   this.loadModule = false;
    // }
  },
  watch: {
    $route(to, from) {
      this.isHome = to.meta.main;
    }
  },
  created() {
    this.isHome = this.$route.meta.main;
    if (localStorage.getItem("HOMEUSERNAME")) {
      this.islogin = true;
    }
  }
}
